/*
 * ZipHandler.java
 *
 * Created on 29 December 2009, 1:57
 */

package fasea.index.filehandler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.lucene.document.Field;
import fasea.utils.FileUtil;
import org.apache.lucene.document.Document;

/**
 *
 * @author gvasil
 */
class ZipHandler extends FileHandler {
    
  public Document getTypedDocument(File file) throws FileHandlerException {

      // Open the ZIP file
      ZipInputStream is = null;
        try {
            is = new ZipInputStream(new FileInputStream(file));
        }
        catch (FileNotFoundException e) {
            throw new FileHandlerException(
                    "File not found: "
                    + file.getAbsolutePath(), e);
      }

      org.apache.lucene.document.Document doc = createDefaultLuceneDocument(file);
      
      String text = null;
      try {
          text = getText(is);
      }
      catch(IOException e) {
          throw new FileHandlerException("Cannot read the zip document", e);
      }
      
      if (text != null && text.trim().length() > 0) {
          doc.add(new Field("content", text, Field.Store.NO,
                  Field.Index.ANALYZED, Field.TermVector.WITH_OFFSETS));
      }

      try {
          is.close();
      } catch (IOException ex) {
          throw new FileHandlerException(ex.getMessage());
      }
      return doc;
  }
  
  private String getText(ZipInputStream is) throws IOException {
      StringBuilder str = new StringBuilder();

      File tmpDir = FileUtil.createTempDir();
      String tmpDirPath = tmpDir.getPath();

      ZipEntry entry = null;
      while ((entry = is.getNextEntry()) != null) {
          String outFileName = null;
          if (entry.isDirectory()) {
              outFileName = tmpDirPath + File.separator + entry.getName();
              File dir = new File(outFileName);
              dir.mkdirs();
          }
          else {
              // unzipping archive
              outFileName = tmpDirPath + File.separator + entry.getName();
              OutputStream out = new FileOutputStream(outFileName);

              byte[] buf = new byte[1024];
              int len;
              while ((len = is.read(buf)) > 0) {
                  out.write(buf, 0, len);
              }

              out.close();
          }

          try {
              LuceneDocumentFactory handler = new LuceneDocumentFactory(false); //store text, not thumbs
              Document doc = handler.getDocument(new File(outFileName));
              if (doc != null) {
                  String content = doc.get("content");
                  if(content != null && !content.isEmpty()){
                    str.append(content).append("\n");
                  }
              }
          } catch (Exception ex) {
              //do nothing
          }
          
      }

      // delete unzipped files
      FileUtil.deleteDir(tmpDir);
      return str.toString();
  }
  
    
}